<template>
  <div class="artical">
    <!-- 工具栏 -->
    <div class="toolbar">
      <div class="left">
        <el-button type="primary" icon="el-icon-plus" @click="routerAdd">添加</el-button>
      </div>
      <div class="right">
        <el-input placeholder="请输入内容" v-model="searchTxt">
          <el-button slot="append" icon="el-icon-search" @click="searchArtical"></el-button>
        </el-input>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="tablebox">
      <el-table border stripe :data="tableData">
        <el-table-column label="编号" prop="articalId"></el-table-column>
        <el-table-column label="文章名称" prop="articalName"></el-table-column>
        <el-table-column label="作者" prop="userName" width="50"></el-table-column>
        <el-table-column label="分类名称" prop="className" width="50"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scoped">
            <el-button v-if="scoped.row.status" type="primary">正常</el-button>
            <el-button v-else type="danger">禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime">
          <template slot-scope="scope">{{ scope.row.createTime | timer }}</template>
        </el-table-column>
        <el-table-column label="创建时间" prop="updateTime">
          <template slot-scope="scope">{{ scope.row.updateTime | times }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">  
            <el-button size="mini" @click="editArtical(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteArtical(scope.row.articalId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域-->
    <div class="page">
      <el-pagination
        hide-on-single-page
        background
        :current-page="articalObj.currentPage"
        :page-sizes="[5,10,15,20]"
        :page-size="articalObj.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getArtical, deleteArtical,getOneArtical} from "@/api";
import {mapMutations} from 'vuex'
export default {
  data() {
    return {
      loading: false,
      searchTxt: "",
      articalObj: {
        currentPage: 1, //当前页
        pageSize: 5 //每页条数
      },
      total: 50, //总条数
      tableData: []
    };
  },
  methods: {
    ...mapMutations('artical',["setArticalObj"]),
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.articalObj.pageSize = val;
      this.articals();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.articalObj.currentPage = val;
      this.articals();
    },
    articals() {
      getArtical(this.articalObj).then(data => {
        // console.log(data);
        if (data.code == 200 && data.msg == "成功") {
          this.tableData = data.data.articals;
          this.total = data.data.total;
        }
      });
    },
    routerAdd() {
      this.$router.push({ name: "addartical" });
    },
    // 编辑
    editArtical(obj){
      // console.log(obj)
      this.setArticalObj(obj);
      this.$router.push({name:"addartical"})
    },
    // 删除
    deleteArtical(id){
      deleteArtical({articalId:id}).then(res =>{
        // console.log(res)
        if(res.code == 200 && res.msg == "删除成功"){
          this.articals();
        }
      })
    },
    searchArtical(){
      var articalId = parseInt(this.searchTxt);
      // console.log(articalId)
      getOneArtical(articalId).then(res =>{
        console.log(res)
        if(res.code == 200 && res.msg == "查询成功"){
          var arr = [];
          arr.push(res.data);
          // console.log(arr)
          this.tableData = arr
        }
      })
    }
  },
  created() {
    // console.log(this.$route);
    this.articals();
  }
};
</script>

<style lang="less" scoped>
.artical {
  width: 100%;
  min-height: calc(100vh - 98px);
  background: #fff;
  padding: 15px;
  .toolbar {
    margin-bottom: 20px;
    overflow: hidden;
    .left {
      float: left;
    }
    .right {
      float: right;
    }
  }
  .page {
    text-align: center;
    margin-top: 20px;
  }
}
</style>